package com.LittleHan.crm.web.listener;

import com.LittleHan.crm.settings.entity.DicValue;
import com.LittleHan.crm.settings.service.DicService;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class SysInitListener implements ServletContextListener {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private WebApplicationContext webApplicationContext;
    DicService dicService;

   @Override
   public void contextInitialized(ServletContextEvent servletContextEvent) {
       ServletContext application=servletContextEvent.getServletContext();
       if(null == webApplicationContext){
           webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
           if(null != webApplicationContext){
               System.out.println("全局作用域对象加载了");
               dicService = (DicService)webApplicationContext.getBean("dicServiceImpl");
               Map<String, List<DicValue>> map=dicService.getListDicValue();
               Set<String> set=map.keySet();
               for (String key:set){
                   System.out.println("key"+key+",value:"+map.get(key));
                   application.setAttribute(key,map.get(key));
               }
           }
       }
   }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

}

